// @flow
import React, { Component } from 'react'
import styles from './VarGrid.css'
import { AgGridReact } from 'ag-grid-react'
import { TableResize } from 'ag-grid-addons'
import VarSettings from './VarSettings'
import LimitSettings from './LimitSettings'

type Props = {
  rowData: Array<Object>,
  refreshVar: Function,
  getVarLimits: Function
}

type States = {
  columnDefs: Array<Object>
}
class VarGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  api: Object
  onGridReady: Function
  TableResize: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      columnDefs: [{
        headerName: '名字',
        field: 'variableName',
        key: 'variableName',
        width: 280,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px'
        }
      }, {
        headerName: '名字en',
        field: 'variableNameEn',
        key: 'variableNameEn',
        width: 180,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: '类型',
        field: 'variableType',
        key: 'variableType',
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: '单位',
        field: 'unit',
        key: 'unit',
        width: 100,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: 'KMX 名字',
        field: 'kmxVariableName',
        key: 'kmxVariableName',
        width: 180,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: 'KMX 名字en',
        field: 'kmxVariableNameEn',
        key: 'kmxVariableNameEn',
        width: 180,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: 'KMX 类型',
        field: 'kmxVariableType',
        key: 'kmxVariableType',
        width: 180,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: 'KMX 单位',
        field: 'kmxUnit',
        key: 'kmxUnit',
        width: 180,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: '最大值',
        field: 'max',
        key: 'max',
        width: 180,
        editable: true,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: '最小值',
        field: 'min',
        key: 'min',
        editable: true,
        width: 180,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: '标准值',
        field: 'standardValue',
        key: 'standardValue',
        width: 180,
        editable: true,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: '更新频率',
        field: 'frequency',
        key: 'frequency',
        editable: true,
        suppressMenu: true,
        cellStyle: {
          'padding': '8px',
          'text-align':'left'
        }
      }, {
        headerName: '阈值',
        field: 'frequency',
        width: 100,
        suppressMenu: true,
        cellRendererParams: {
          refreshLimit: this.props.getVarLimits
        },
        cellRendererFramework: LimitSettings,
        cellStyle: {
          'padding': '8px',
          'text-align':'left',
          'color': '#f00'
        }
      }, {
        headerName: '操作',
        minWidth: 240,
        maxWidth: 240,
        cellStyle: {
          'padding': '8px',
          'text-align': 'left'
        },
        cellRendererParams: {
          refreshVar: this.props.refreshVar,
          refreshLimit: this.props.getVarLimits
        },
        cellRendererFramework: VarSettings
      }]
    }
    this.gridOptions = {
      pagination: false,
      animation: true,
      rowSelection: 'single',
      enableColResize: true,
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      // onSelectionChanged: () => {
      //   const selections = this.api.getSelectedRows()
      //   const { getVarLimits } = this.props
      //   console.log(selections)
      //   getVarLimits(selections[0])
      // },
      onModelUpdated: () => {
        if (this.api) {
          this.api.sizeColumnsToFit()
          this.TableResize()
        }
      }
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
  }

  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }
  render () {
    const { rowData } = this.props
    const { columnDefs } = this.state
    return (
      <div className={styles['grid'] + ' ag-fresh'} >
        <AgGridReact {...{
          gridOptions: this.gridOptions,
          rowHeight: '34',
          headerHeight: '28',
          columnDefs: columnDefs,
          rowData: rowData,
          onGridReady: this.onGridReady
        }} />
      </div>
    )
  }
}

export default VarGrid
